package com.ror.java.se.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo03 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> ft1 = new FutureTask<>(new MyCallable());
        Thread t1 = new Thread(ft1);
        t1.start();
        FutureTask<Integer> ft2 = new FutureTask<>(new MyCallable());
        Thread t2 = new Thread(ft2);
        t2.start();
        Integer res1 = ft1.get();
        System.out.println(res1);
        Integer res2 = ft2.get();
        System.out.println(res2);
    }

    private static class MyCallable implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            int sum = 0;
            for (int i = 1; i <= 100; i++) {
                sum += i;
                System.out.println(Thread.currentThread().getName() + ": " + sum);
            }
            return sum;
        }
    }
}
